package bist.believeinstep.impl;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import bist.believeinstep.dao.AudioDAO;
import bist.believeinstep.dao.ConnectionDAO;
import bist.believeinstep.domain.Audio;
import bist.believeinstep.mapper.AudioMapper;
import bist.believeinstep.util.Constant.MediaType;
import bist.believeinstep.util.Constant.State;

public class AudioTemplate implements ConnectionDAO, AudioDAO{
	private JdbcTemplate jdbcTemplate;

	@Override
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	
	@Override
	public boolean create(Audio audio) {
		String query = "INSERT INTO bist_audios(code, name, introduction, descriptions, link, state, type,"
				+ "created_date, modified_date, created_by, modified_by)" +
				" VALUES(?,?,?,?,?,?,?,?,?,?,?)";
		try {
			jdbcTemplate.update(query, new Object[]{
				audio.getCode(),
				audio.getName(),
				audio.getIntroduction(),
				audio.getDescriptions(),
				audio.getLink(),
				audio.getState().name(),
				audio.getType().name(),
				audio.getCreated(),
				audio.getModified(),
				audio.getCreatedBy(),
				audio.getModifiedBy()
			});
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean update(Audio audio) {
		String query = "UPDATE bist_audios" +
				" SET code = ?" +
				" name = ?" +
				" introduction = ?" +
				" descriptions = ?" +
				" link = ?" +
				" state = ?" +
				" type = ?" +
				" created_date = ?"+
				" modified_date = ?"+
				" created_by = ?"+
				" modified_by = ?";
		try {
			jdbcTemplate.update(query, new Object[]{
				audio.getCode(),
				audio.getName(),
				audio.getIntroduction(),
				audio.getDescriptions(),
				audio.getLink(),
				audio.getState().name(),
				audio.getType().name(),
				audio.getModified(),
				audio.getModifiedBy(),
				audio.getId()
			});
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean update(int id, MediaType type) {
		String query = "UPDATE bist_audios" +
				" SET type = ?" +
				" WHERE id = ?";
		try {
			jdbcTemplate.update(query, new Object[]{type.name(), id});
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return false;
	}

	@Override
	public boolean update(int id, State state) {
		String query = "UPDATE bist_audios" +
				" SET state = ?" +
				" WHERE id = ?";
		try {
			jdbcTemplate.update(query, new Object[]{state.name(), id});
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return false;
	}

	@Override
	public boolean delete(int id) {
		String query = "DELETE bist_audios WHERE id = ?";
		try {
			jdbcTemplate.update(query, new Object[]{id});
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean delete(String code) {
		String query = "DELETE bist_audios WHERE code = ?";
		try {
			jdbcTemplate.update(query, new Object[]{code});
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public Audio find(int id) {
		String query = "SELECT * FROM bist_audios WHERE id = ?";
		try {
			Audio audio = jdbcTemplate.queryForObject(query, new Object[]{id}, new AudioMapper());
			return audio;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public Audio find(String code) {
		String query = "SELECT * FROM bist_audios WHERE code = ?";
		try {
			Audio audio = jdbcTemplate.queryForObject(query, new Object[]{code}, new AudioMapper());
			return audio;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<Audio> find(MediaType type) {
		String query = "SELECT * FROM bist_audios WHERE type = ?";
		try {
			List<Audio> audios = jdbcTemplate.query(query, new Object[]{type.name()}, new AudioMapper());
			return audios;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<Audio> find(State state) {
		String query = "SELECT * FROM bist_audios WHERE state = ?";
		try {
			List<Audio> audios = jdbcTemplate.query(query, new Object[]{state.name()}, new AudioMapper());
			return audios;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<Audio> findAll() {
		String query = "SELECT * FROM bist_audios";
		try {
			List<Audio> audios = jdbcTemplate.query(query, new AudioMapper());
			return audios;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
